import random
import openpyxl
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import yaml
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

class BoxDriver:


    def __init__(self,broswer='Chrome',url="") -> None:
        if broswer == 'Chrome':
            self.driver = webdriver.Chrome()
        elif broswer == 'Firefox':
            self.driver = webdriver.Firefox()
        elif broswer == 'Edge':
            self.driver = webdriver.Edge()
        elif broswer == 'Opera':
            self.driver = webdriver.Opera()
        elif broswer == 'Safari':
            self.driver = webdriver.Safari()
        elif broswer == 'Ie':
            self.driver = webdriver.Ie()
        self.get(url)
        self.maximize_window()
        self.driver.implicitly_wait(5)

    def get(self,url):
        """  
        打开指定的页面
        url     页面地址
        """
        self.driver.get(url)

    def maximize_window(self):
        """
        窗口最大化
        """
        self.driver.maximize_window()

    def implicitly_wait(self,seconds):
        """
        隐式等待
        seconds     最大等待时间，单位是秒
        """
        self.driver.implicitly_wait(seconds)

    def input(self,by,value,text):
        """  
        向元素中写入内容
        by      定位方式
        value   定位值
        text    写入的内容
        """
        e = self.driver.find_element(by,value)
        e.clear()
        e.send_keys(text)

    def click(self,by,value):
        """  
        单击元素
        by      定位方式
        value   定位值
        """
        self.driver.find_element(by,value).click()

    def switch_to_frame(self,by,value):
        """  
        进入iframe元素
        """
        iframe = self.driver.find_element(by,value)
        self.driver.switch_to.frame(iframe)

    def select_by_index(self,by,by_value,index):
        """  
        根据index进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by,by_value)
        depts = Select(select)
        depts.select_by_index(index)    # 根据下标选择，下标从0开始

    def select_by_value(self,by,by_value,value):
        select = self.driver.find_element(by, by_value)
        depts = Select(select)
        depts.select_by_value(value)  # 根据下标选择，下标从0开始

    def select_by_visible_text(self,by,by_value,visible_text):
        select = self.driver.find_element(by, by_value)
        depts = Select(select)
        depts.select_by_visible_text(visible_text)  # 根据下标选择，下标从0开始

    def find_elements(self,by,value):
        """  
        定位多个元素
        by      定位方式
        by_value   定位值   
        """
        return self.driver.find_elements(by,value)

    def find_element(self,by,value):
        """  
        定位单个元素
        by      定位方式
        by_value   定位值   
        """
        return self.driver.find_element(by,value)

    def close(self):
        """  
        关闭当前窗口
        """
        self.driver.close()
    def getRealname(self):
        return self.find_element(By.XPATH,'//*[@id="mainNavbar"]/div/ul[1]/li/a').text

    def tip(self):
        return self.driver.find_element(By.XPATH,'/html/body/div[2]/div/div/div[1]/div').text

    def confirm(self):
        self.driver.click(By.XPATH, '//*[text()="确认"]')

    def findElementByText(self,text):
        return self.driver.find_element_by_link_text(text)


class BasePage:

    def __init__(self,driver:BoxDriver) -> None:
        self.driver = driver

    @staticmethod # 静态方法
    def get_yaml(path):
        """  
        解析yaml配置文件
        path    yaml文件地址
        """
        with open(path,'r',encoding='utf-8') as file:
            content = file.read()
            return yaml.safe_load(content)

    @staticmethod
    def get_excel(path,sheetname):
        """  
        解析excel数据文件
        path    文件地址
        return [(1,2),(3,4)]
        """
        # 加载工作簿
        workbook = openpyxl.load_workbook(path)
        # 获取工作簿表
        worksheet = workbook[sheetname]
        return list(worksheet.values)[1:]

    @staticmethod
    def send_mail(path,server,sender,code,receivers,simple,content):
        """
          path
          sender 发件人
          code 授权码
          receivers 收件人
          server 设置邮件服务器地址
          simple 主题
          content 生成正文
        """

        # 服务器端口号
        port = 25
        """ 创建邮件 """
        # 创建邮件对象
        mail = MIMEMultipart()
        # 添加发件人
        mail['from'] = sender
        # 添加收件人
        mail['to'] = receivers
        # 添加主题
        mail['subject'] = simple

        """ 添加附件 """
        # 读取附件
        with open(path,'rb') as file:
            report = file.read()
        # 对附件进行编码
        attchment = MIMEText(report,'base64','utf-8')
        # 设置附件的类型
        attchment['Content-Type'] = 'application/octet-stream'
        # 设置附件的处理方式
        attchment['Content-Disposition'] = 'attachment;filename=%s'%path.split('/')[-1]
        # 添加附件
        mail.attach(attchment)

        """ 添加正文 """
        # 对正文进行编码
        body = MIMEText(content,'html','utf-8')
        # 添加正文
        mail.attach(body)
        """ 发送邮件 """
        # 创建smtp对象
        smtp = smtplib.SMTP()
        # 连接服务器
        smtp.connect(server,port)
        # 登陆服务器
        smtp.login(sender,code)
        # 发送邮件
        smtp.sendmail(sender,receivers.split(';'),mail.as_string())
        # 关闭服务器
        smtp.close()
        print('邮件发送完毕！')

if __name__ == '__main__':
    BasePage.send_mail('../resources/qq邮箱发送邮件的码.txt','smtp.qq.com','1131896900@qq.com','kgdyrppdjukphhcc','zd408032656@163.com;1131896900@qq.com','Ranzhi自动化测试报告！','本文件接收人请好好查看测试报告！！')